package com.lht.admin.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @author cs12110 create at 2019-12-29 20:05
 * <p>
 * @since 1.0.0
 */
public class ListUtil {

    public static <T> T getFirstElement(List<T> list) {
        return isEmpty(list) ? null : list.get(0);
    }

    public static boolean isNotEmpty(List<?> list) {
        return null != list && list.size() > 0;
    }

    public static boolean isEmpty(List<?> list) {
        return null == list || list.isEmpty();
    }


    public static <T> List<T> buildList() {
        int defSize = 10;
        return buildList(defSize);
    }

    public static <T> List<T> buildList(int size) {
        return new ArrayList<>(size);
    }


    /**
     * fix list, if list == null then return Collections.emptyLis();
     *
     * @param list list
     * @param <T>  type
     * @return List
     */
    public static <T> List<T> fixList(List<T> list) {
        if (isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }


    /**
     * 去除null元素
     *
     * @param originList origin list
     * @param <T>        type of list element
     * @return list
     */
    public static <T> List<T> removeNullElement(List<T> originList) {
        if (isEmpty(originList)) {
            return Collections.emptyList();
        }

        return originList.stream().filter(Objects::nonNull).collect(Collectors.toList());
    }
}
